package com.dj.mingkong.portal.biz.controller.music;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dj.mingkong.common.annotation.RequestLimit;
import com.dj.mingkong.common.infrastructure.base.dto.page.PageVo;
import com.dj.mingkong.common.infrastructure.base.dto.rest.RestResponse;
import com.dj.mingkong.common.microservice.UserContext;
import com.dj.mingkong.common.util.AssertUtils;
import com.dj.mingkong.common.util.SmartBeanCopy;
import com.dj.mingkong.portal.biz.controller.music.vo.request.AlbumCollectAddVo;
import com.dj.mingkong.portal.biz.controller.music.vo.request.AlbumCollectCancelVo;
import com.dj.mingkong.portal.biz.controller.music.vo.request.AlbumCollectQueryVo;
import com.dj.mingkong.portal.biz.controller.music.vo.response.AlbumCollectDetailVo;
import com.dj.mingkong.portal.biz.controller.music.vo.response.AlbumCollectVo;
import com.dj.mingkong.portal.biz.service.music.AlbumCollectService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * 专辑收藏管理
 *
 * @author zenglonghui
 * @date: 2022/8/24 23:32
 */
@RestController
@RequestMapping("/album/collect")
public class AlbumCollectController {
    @Resource
    private AlbumCollectService albumCollectService;

    /**
     * 添加专辑收藏
     *
     * @param addVo {}
     * @return Boolean
     */
    @PostMapping("/add")
    @RequestLimit(count = 5)
    public RestResponse<Boolean> add(@RequestBody @Validated AlbumCollectAddVo addVo) {
        albumCollectService.add(addVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 收藏专辑
     *
     * @param addVo 实例对象
     * @return 是否成功，true-成功，false-失败
     * @throws RuntimeException albumCollectVo.getAlbumId为空的时候会抛出运行时异常
     */
    @PostMapping("/handle")
    @RequestLimit(count = 10)
    public RestResponse<Boolean> handle(@RequestBody @Validated AlbumCollectAddVo addVo) {
        albumCollectService.handle(addVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 取消收藏
     *
     * @param cancelVo {}
     * @return Boolean
     */
    @PostMapping("/cancel")
    @RequestLimit(count = 5)
    public RestResponse<Boolean> cancel(@RequestBody @Validated AlbumCollectCancelVo cancelVo) {
        albumCollectService.cancel(cancelVo);
        return RestResponse.buildSuccess();
    }

    /**
     * 获取专辑收藏信息
     *
     * @param albumId 专辑id
     * @return 是否成功，true-成功，false-失败
     */
    @GetMapping("/getAlbumCollect/{albumId}")
    public RestResponse<AlbumCollectDetailVo> getAlbumCollect(@PathVariable("albumId") Long albumId) {
        return RestResponse.success(albumCollectService.getAlbumCollect(albumId));
    }

    /**
     * 分页查询我收藏的专辑列表
     *
     * @param queryVo {}
     * @return AlbumVo
     */
    @PostMapping("/pageByVo")
    public RestResponse<PageVo<AlbumCollectVo>> pageByVo(@RequestBody AlbumCollectQueryVo queryVo) {
        AssertUtils.checkQueryPageVo(queryVo);
        queryVo.setUserId(UserContext.getUserId());
        final Page<AlbumCollectVo> albumCollectVoPage = albumCollectService.pageByVo(queryVo);
        final PageVo<AlbumCollectVo> pageVo = SmartBeanCopy.copyPageVo(albumCollectVoPage, AlbumCollectVo.class);
        return RestResponse.success(pageVo);
    }
}
